package com.zhx.factory.support;

import com.zhx.factory.BeanFactory;
import com.zhx.factory.BeansException;
import com.zhx.factory.config.BeanDefinition;

/**
 * @author zhx
 * @title 定义模版
 * @description
 * @date 2022/9/29 10:49
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
    @Override
    public Object getBean(String name) throws BeansException {
        return doGetBean(name, null);
    }
    @Override
    public Object getBean(String name, Object... args) throws BeansException {
        return doGetBean(name, args);
    }

    public Object doGetBean(String name, Object... args) {
        //查看缓存中是否存在，有就直接拿，没有子类自己实现
        Object bean = getSingleton(name);
        if (bean != null) {
            return bean;
        }

        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name, beanDefinition,args);
    }

    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException;

}
